package org.alerve.android.resourcebuilder.modules;

import java.util.ArrayList;
import java.util.List;

import org.alerve.android.resourcebuilder.IModule;

/**
 * 
 * This abstract builder ads the Decorator pattern to {@link IModule} to chain
 * many into one.
 * 
 * @author pabs87@gmail.com
 * 
 * @param <Type>
 */
public abstract class DecoratorModule<Type> implements IModule<Type> {

	private List<IModule<Type>> modules;

	public boolean build(Type subject) {
		for (IModule<Type> builder : getModules()) {
			if (!builder.build(subject)) {
				return false;
			}
		}
		return true;
	}

	public List<IModule<Type>> getModules() {
		if (modules == null) {
			modules = new ArrayList<IModule<Type>>();
		}
		return modules;
	}

	/**
	 * 
	 * Method used to add builders to be used in {@link IModule#build(Object)}
	 * 
	 * @param builder
	 */
	protected void addModule(IModule<Type> builder) {
		getModules().add(builder);
	}

}
